﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Kinematic
{
    public class Fisica
    {
        //Start :: Posicion Inicial
        //End   :: Posicion Final
        //muzzle_v :: es la Rapidez con la que sale el proyectil
        //Gravity :: Vector que contiene el componente de gravedad en el y
        public Vector3? calculateFiringSolution(Vector3 Start, Vector3 End, float muzzle_v, Vector3 gravity)
        {

            Vector3 Delta = End - Start;

            float a = gravity.LengthSquared();
            float b = -4.0f * (Vector3.Dot(gravity, Delta) + muzzle_v * muzzle_v);
            float c = 4.0f * Delta.LengthSquared();

            if (4.0f * a * c > b * b)
                return null;

            float time0 = (float)Math.Sqrt((-b + Math.Sqrt(b * b - 4 * a * c)) / (2.0f * a));
            float time1 = (float)Math.Sqrt((-b - Math.Sqrt(b * b - 4 * a * c)) / (2.0f * a));
            float ttt;

            if (time0 < 0)
                if (time1 < 0)
                    return null;
                else
                    ttt = time1;
            else
                if (time1 < 0)
                    ttt = time0;
                else
                    ttt = Math.Min(time0, time1);

            return (2.0f * Delta - gravity * ttt * ttt) / (2.0f * muzzle_v * ttt);
        }


    }
}
